﻿@{
    Layout = "~/Shared/Layouts/_AdminLayout.cshtml";
    Page.Title = "Администраторски сметки";
    
    var userToAdd = "";
    var role = "Администратор";
    
    if(IsPost)
    {
        userToAdd = Request["userToAdd"];
        
        if(userToAdd != null)
        {
            // Add User to Role
            if (WebSecurity.UserExists(userToAdd) &&
                !Roles.IsUserInRole(userToAdd, role))
            {
                Roles.AddUsersToRoles(
                    new [] { userToAdd }.ToArray(),
                    new [] { role }.ToArray()
                );
            }
            else
            {
                ModelState.AddError("userToAdd", "Неуспешно додавање на сметка");
            }
        }
        else if (Request["userToRemove"] != null)
        {
            // Remove User from Role
            
            var userToRemove = Request["userToRemove"];
            if (Roles.IsUserInRole(userToRemove, role))
            {
                Roles.RemoveUsersFromRoles(
                    new [] { userToRemove }.ToArray(),
                    new [] { role }.ToArray()
                );
            }
        }
    }
    
    var users = Roles.GetUsersInRole(role);
}

<h1>Администраторски сметки</h1>

<table class="grid">
    <tr>
        <th colspan="3" class="gridHeader">Сметка</th>
    </tr>
    @foreach (var user in users)
    {
        <tr>
            <td id="mainColumn">
                @user
            </td>
            <td>
                @{
                    var buttonState = "";
                    if(user == WebSecurity.CurrentUserName)
                    {
                        buttonState = "disabled=disabled";
                    }
                }
                    <form action="" method="post">
                        @Html.Hidden("userToRemove", user)
                        <input type="submit" value="Одстрани улога" @buttonState />
                    </form>

            </td>
        </tr> 
    }
</table>

<h2>Додади администраторска сметка</h2>
<form action="" method="post">
    <p>
        Корисничко име:
        @Html.TextBox("userToAdd", userToAdd)
        <input type="submit" value="Додади" />
        @Html.ValidationMessage("userToAdd")
    </p>
</form>